/*
 * Copyright (c) 2025 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <cstdint>
#include <gtest/gtest.h>
#include <string>
#include <vector>

#include "arkweb_preload_common.cpp"
#include "parameters.h"

using namespace testing::ext;
 
namespace OHOS::ArkWeb {

class ArkWebPreloadTest : public testing::Test {
public:
    static void SetUpTestCase(void);
    static void TearDownTestCase(void);
    void SetUp();
    void TearDown();
};

void ArkWebPreloadTest::SetUpTestCase(void)
{}

void ArkWebPreloadTest::TearDownTestCase(void)
{}

void ArkWebPreloadTest::SetUp(void)
{}

void ArkWebPreloadTest::TearDown(void)
{}

/**
 * @tc.name: ArkWebPreloadTest_GetArkwebBundleInstallLibPath_001.
 * @tc.desc: Test the GetArkwebBundleInstallLibPath.
 * @tc.type: FUNC
 * @tc.require:
 */
HWTEST_F(ArkWebPreloadTest, ArkWebPreloadTest_GetArkwebBundleInstallLibPath_001, TestSize.Level1) {
    int webEngineEnforce = OHOS::system::GetIntParameter("web.engine.enforce", 0);
    OHOS::system::SetParameter("web.engine.enforce", std::to_string(0));
    setActiveWebEngineVersion(ArkWebEngineVersion::M114);
 
    auto arkwebLibPath = GetArkwebBundleInstallLibPath();
    OHOS::system::SetParameter("web.engine.enforce", std::to_string(webEngineEnforce));
}

/**
 * @tc.name: ArkWebPreloadTest_GetArkwebBundleInstallLibPath_002.
 * @tc.desc: Test the GetArkwebBundleInstallLibPath.
 * @tc.type: FUNC
 * @tc.require:
 */
HWTEST_F(ArkWebPreloadTest, ArkWebPreloadTest_GetArkwebBundleInstallLibPath_002, TestSize.Level1) {
    int webEngineEnforce = OHOS::system::GetIntParameter("web.engine.enforce", 0);
    OHOS::system::SetParameter("web.engine.enforce", std::to_string(0));
    setActiveWebEngineVersion(ArkWebEngineVersion::M132);
 
    auto arkwebLibPath = GetArkwebBundleInstallLibPath();
    OHOS::system::SetParameter("web.engine.enforce", std::to_string(webEngineEnforce));
}

/**
 * @tc.name: ArkWebPreloadTest_GetArkwebBundleInstallLibPath_003.
 * @tc.desc: Test the GetArkwebBundleInstallLibPath.
 * @tc.type: FUNC
 * @tc.require:
 */
HWTEST_F(ArkWebPreloadTest, ArkWebPreloadTest_GetArkwebBundleInstallLibPath_003, TestSize.Level1) {
    int webEngineEnforce = OHOS::system::GetIntParameter("web.engine.enforce", 0);
    OHOS::system::SetParameter("web.engine.enforce", std::to_string(0));
    setActiveWebEngineVersion(ArkWebEngineVersion::SYSTEM_EVERGREEN);
 
    auto arkwebLibPath = GetArkwebBundleInstallLibPath();
    OHOS::system::SetParameter("web.engine.enforce", std::to_string(webEngineEnforce));
}

/**
 * @tc.name: ArkWebPreloadTest_GetArkwebBundleInstallLibPath_004.
 * @tc.desc: Test the GetArkwebBundleInstallLibPath.
 * @tc.type: FUNC
 * @tc.require:
 */
HWTEST_F(ArkWebPreloadTest, ArkWebPreloadTest_GetArkwebBundleInstallLibPath_004, TestSize.Level1) {
    std::string pkgName = OHOS::system::GetParameter("persist.arkwebcore.package_name", "");
    std::cout << "dzin 4 pkgname: " << pkgName << std::endl;
    OHOS::system::SetParameter("persist.arkwebcore.package_name", "");    
    auto arkwebLibPath = GetArkwebBundleInstallLibPath();
    OHOS::system::SetParameter("persist.arkwebcore.package_name", pkgName);    
    EXPECT_TRUE(arkwebLibPath.empty());
}

/**
 * @tc.name: ArkWebPreloadTest_GetArkwebBundleInstallLibPath_005.
 * @tc.desc: Test the GetArkwebBundleInstallLibPath.
 * @tc.type: FUNC
 * @tc.require:
 */
HWTEST_F(ArkWebPreloadTest, ArkWebPreloadTest_GetArkwebBundleInstallLibPath_005, TestSize.Level1) {
    std::string pkgName = OHOS::system::GetParameter("persist.arkwebcore.package_name", "");
    OHOS::system::SetParameter("persist.arkwebcore.package_name", "com.ohos.arkwebcore");    
    auto arkwebLibPath = GetArkwebBundleInstallLibPath();
    OHOS::system::SetParameter("persist.arkwebcore.package_name", pkgName);    
    EXPECT_TRUE(arkwebLibPath.empty());
}

/**
 * @tc.name: ArkWebPreloadTest_GetArkwebBundleInstallLibPath_006.
 * @tc.desc: Test the GetArkwebBundleInstallLibPath.
 * @tc.type: FUNC
 * @tc.require:
 */
HWTEST_F(ArkWebPreloadTest, ArkWebPreloadTest_GetArkwebBundleInstallLibPath_006, TestSize.Level1) {
    std::string pkgName = OHOS::system::GetParameter("persist.arkwebcore.package_name", "");
    OHOS::system::SetParameter("persist.arkwebcore.package_name", "com.huawei.hmos.arkwebcore");    
    auto arkwebLibPath = GetArkwebBundleInstallLibPath();
    OHOS::system::SetParameter("persist.arkwebcore.package_name", pkgName);    
    EXPECT_TRUE(arkwebLibPath.empty());
}

/**
 * @tc.name: ArkWebPreloadTest_GetOhosAdptGlueSrcLibPath_001.
 * @tc.desc: Test the GetOhosAdptGlueSrcLibPath.
 * @tc.type: FUNC
 * @tc.require:
 */
HWTEST_F(ArkWebPreloadTest, ArkWebPreloadTest_GetOhosAdptGlueSrcLibPath_001, TestSize.Level1) {
    auto ohosAdptGlueSrcLibPath = GetOhosAdptGlueSrcLibPath();
#if defined(webview_x86_64)
    EXPECT_TRUE(ohosAdptGlueSrcLibPath.empty());
#else
    EXPECT_FALSE(ohosAdptGlueSrcLibPath.empty());
#endif  
}

/**
 * @tc.name: ArkWebPreloadTest_PreloadArkWebEngineLib_001.
 * @tc.desc: Test the PreloadArkWebEngineLib.
 * @tc.type: FUNC
 * @tc.require:
 */
HWTEST_F(ArkWebPreloadTest, ArkWebPreloadTest_PreloadArkWebEngineLib_001, TestSize.Level1) {
    // set package name to empty
    std::string pkgName = OHOS::system::GetParameter("persist.arkwebcore.package_name", "");
    OHOS::system::SetParameter("persist.arkwebcore.package_name", "");    
    PreloadArkWebEngineLib();
    // reset package name
    OHOS::system::SetParameter("persist.arkwebcore.package_name", pkgName);     
}

/**
 * @tc.name: ArkWebPreloadTest_PreloadArkWebEngineLib_002.
 * @tc.desc: Test the PreloadArkWebEngineLib.
 * @tc.type: FUNC
 * @tc.require:
 */
HWTEST_F(ArkWebPreloadTest, ArkWebPreloadTest_PreloadArkWebEngineLib_002, TestSize.Level1) {
    // set package name to invalid
    std::string pkgName = OHOS::system::GetParameter("persist.arkwebcore.package_name", "");
    OHOS::system::SetParameter("persist.arkwebcore.package_name", "com.ohos.arkwebcore");    
    PreloadArkWebEngineLib();
    // reset package name
    OHOS::system::SetParameter("persist.arkwebcore.package_name", pkgName);     
}

/**
 * @tc.name: ArkWebPreloadTest_PreloadArkWebEngineLib_001.
 * @tc.desc: Test the PreloadArkWebEngineLib.
 * @tc.type: FUNC
 * @tc.require:
 */
HWTEST_F(ArkWebPreloadTest, ArkWebPreloadTest_PreloadArkWebEngineLib_003, TestSize.Level1) {
    // set package name to empty
    std::string pkgName = OHOS::system::GetParameter("persist.arkwebcore.package_name", "");
    OHOS::system::SetParameter("persist.arkwebcore.package_name", "com.huawei.hmos.arkwebcore");    
    PreloadArkWebEngineLib();
    // reset package name
    OHOS::system::SetParameter("persist.arkwebcore.package_name", pkgName);     
}

/**
 * @tc.name: ArkWebPreloadTest_PreloadOHOSAdptGlueSrcLib_001.
 * @tc.desc: Test the PreloadOHOSAdptGlueSrcLib.
 * @tc.type: FUNC
 * @tc.require:
 */
HWTEST_F(ArkWebPreloadTest, ArkWebPreloadTest_PreloadOHOSAdptGlueSrcLib_001, TestSize.Level1) {
    PreloadOHOSAdptGlueSrcLib();
}

/**
 * @tc.name: ArkWebPreloadTest_GetSysTotalRAMInfo_001.
 * @tc.desc: Test the GetSysTotalRAMInfo.
 * @tc.type: FUNC
 * @tc.require:
 */
HWTEST_F(ArkWebPreloadTest, ArkWebPreloadTest_GetSysTotalRAMInfo_001, TestSize.Level1)
{
    int ret = GetSysTotalRAMInfo();
    EXPECT_TRUE(ret >= 0);
}

/**
 * @tc.name: ArkWebPreloadTest_GetRenderPreLoadMode_001.
 * @tc.desc: Test the GetRenderPreLoadMode.
 * @tc.type: FUNC
 * @tc.require:
 */
HWTEST_F(ArkWebPreloadTest, ArkWebPreloadTest_GetRenderPreLoadMode_001, TestSize.Level1) {
    // 8G RAM device
    int ret = GetRenderPreLoadMode(8 * 1024);
    EXPECT_TRUE(ret >= 0);
    // 12G RAM device
    ret = GetRenderPreLoadMode(12 * 1024);
    EXPECT_TRUE(ret >= 0);
    // 16G RAM device
    ret = GetRenderPreLoadMode(16 * 1024);
    EXPECT_TRUE(ret >= 0);
}

/**
 * @tc.name: ArkWebPreloadTest_PreloadArkWebLibForRender_001.
 * @tc.desc: Test the PreloadArkWebLibForRender.
 * @tc.type: FUNC
 * @tc.require:
 */
HWTEST_F(ArkWebPreloadTest, ArkWebPreloadTest_PreloadArkWebLibForRender_001, TestSize.Level1) {
    PreloadArkWebLibForRender();
    
    int preloadMode = 0;
    PreloadArkWebLibForRender(preloadMode);

    preloadMode = 1;
    PreloadArkWebLibForRender(preloadMode);

    preloadMode = 2;
    PreloadArkWebLibForRender(preloadMode);
}
}